package com.turing.post.test;

import java.io.Reader;
import java.io.StringReader;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class tranXML {
	public static void main(String[] args) throws Exception {
		SAXBuilder builder = new SAXBuilder();
		Document doc = null;
		String textXml = "<?xml version=\"1.0\" encoding=\"gb18030\" ?>"
				+ "<TransRequest>" + "<TranCode>9988</TranCode>"
				+ "<MerchantNo>961850</MerchantNo>" + "<ReqBody>" + "<Paid>"
				+ "<Amount>1000.0</Amount>" + "<CardNum>1</CardNum>"
				+ "<Card>" + "<CardNo>卡号(可选)</CardNo>"
				+ "<Track2>二磁道信息(可选)</Track2>" + "<Track3>三磁道信息(可选)</Track3>"
				+ "<PaidPasswd>支付密码明文</PaidPasswd>"
				+ "<Amount>支付发生额(分)</Amount>" + "</Card>" + "</Paid>"
				+ "</ReqBody>" + "<TranMac>交易MAC</TranMac>" + "</TransRequest>";
		Reader in = new StringReader(textXml);
		try {
			doc = builder.build(in);
			Element root = doc.getRootElement();
			System.out.println(root.getName());
			Element tranCode_el = root.getChild("TranCode");
			Element tranMac_el = root.getChild("TranMac");
			System.out.println(tranCode_el.getText());
			System.out.println(tranMac_el.getText());
			Element reqBody_el = root.getChild("ReqBody");
			Element paid_el = reqBody_el.getChild("Paid");
			Element cardNum_el = paid_el.getChild("CardNum");
			System.out.println( "cardNum == " + cardNum_el.getTextTrim());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
